<?php

/**
 * SigmaCMS - Content Management System
 *
 * Copyright (C) 2008 Pavol Biely
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 * @package    SigmaCMS
 * @author     Pavol Biely <pavolbiely@gmail.com>
 * @copyright  2008 Pavol Biely
 * @license    http://www.gnu.org/licenses/gpl.txt   GNU General Public License
 * @link       http://pabi3.com/
 * @since      0.1.0
 */

abstract class CreateTables
{
	/**
	 * Nastaví počiatočné dáta
	 *
	 * @return void
	 */
	static private function data($sqlite, $prefix)
	{
		$lang    = $_SESSION['language'];
		$general = $_SESSION['general'];

		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('skin', 'default/')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('skin_admin', 'default.css')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('h1', '".sqlite_escape_string($general['h1'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('title', '".sqlite_escape_string($general['title'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('language', '".sqlite_escape_string($lang)."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('description', '".sqlite_escape_string($general['description'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('keywords', '".sqlite_escape_string($general['keywords'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('cookie_nameprefix', '".sqlite_escape_string($general['cookie_nameprefix'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('cookie_expiration', '".sqlite_escape_string($general['cookie_expiration'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('cookie_path', '".sqlite_escape_string($general['cookie_path'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('cookie_domain', '".sqlite_escape_string($general['cookie_domain'])."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('cookie_secure', '".($general['cookie_secure'] == 'true' ? '1' : '0')."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}configuration VALUES ('cookie_httponly', '".($general['cookie_httponly'] == 'true' ? '1' : '0')."')",$sqlite);
		sqlite_query("INSERT INTO {$prefix}languages VALUES (1, 'sk');",$sqlite);
		sqlite_query("INSERT INTO {$prefix}languages VALUES (2, 'en');",$sqlite);
		sqlite_query("INSERT INTO {$prefix}members VALUES ('1', 'admin', '8450eca01665516d9aeb5317764902b78495502637c96192c81b1683d32d691a0965cf037feca8b9ed9ee6fc6ab8f27fce8f77c4fd9b4a442a00fc317b8237e6', 1, 0);",$sqlite);
		sqlite_query("INSERT INTO {$prefix}members_groups VALUES (1, 'Admin');",$sqlite);
		sqlite_query("INSERT INTO {$prefix}members_groups VALUES (2, 'Members');",$sqlite);
		sqlite_query("INSERT INTO {$prefix}skins VALUES ('index', 'application/xhtml+xml', 'default')",$sqlite);
	}
	
	/**
	 * Vytvoriť tabuľky
	 *
	 * @return void
	 */
	static public function begin()
	{
		// SQL
		$query = file_get_contents(dirname(__FILE__).'/sqlite.sql');

		// pridať prefix
		$prefix = $_SESSION['database']['prefix'];

		// pridať prefixy
		$query  = preg_replace(
		"/CREATE\s+TABLE\s+([a-z][a-z0-9_]+)\s+\(/i",
		"CREATE TABLE {$prefix}$1 (",
		$query);

		$dbfile = dirname(__FILE__).'/../../'.$_SESSION['database']['database'];

		$sqlite = sqlite_open($dbfile,0666);

		sqlite_query($sqlite,$query);

		self::data($sqlite, $prefix);

		#echo "<p>".sqlite_error_string(sqlite_last_error($sqlite))."</p>"; // DEBUG
		sqlite_close($sqlite);
	}
}
